package com.lx.chapter1.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建一个servlet类
 *
 * @author 段誉
 * @create 2019-03-21 19:51
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

  /**
   * 执行get请求
   * @param req
   * @param resp
   * @throws ServletException
   * @throws IOException
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentTime = sdf.format(new Date());
    req.setAttribute("currentTime", currentTime);
    req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp")
            .forward(req, resp);
  }
}
